define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'flow/index' + location.search,
                    add_url: 'flow/add',
                    edit_url: 'flow/edit',
                    del_url: 'flow/del',
                    multi_url: 'flow/multi',
                    import_url: 'flow/import',
                    table: 'flow',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'main_type', title: __('Main_type'), searchList: {"1":__('Main_type 1'),"2":__('Main_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'main_id', title: __('Main_id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'reward', title: __('Reward'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('change', "#c-policy", function () {
                let val = $(this).val();
                Fast.api.ajax({
                    url: 'flow/ajax',
                    data: {id: val},
                    loading: false
                }, function (data) {
                    let flow = data['flow'];
                    $("#c-reward").removeAttr("data-rule");
                    $("#c-reward").attr("data-rule","required&range(0~"+flow['reward']+")");

                    $("#c-product_id").val(data.product_id);
                    $("#c-product_id").selectPageRefresh();
                    return false;
                }, function () {
                    return false;
                });
            });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
